package cn.sytton.taffe.framework.thread;

import cn.sytton.taffe.framework.util.SpringContextUtil;
import cn.sytton.taffe.framework.util.Threads;

import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 异步任务管理器
 * 
 * @author skyrock
 */
public class AsyncManager {

    /**
     * 异步操作任务调度线程池
     */
    private final ScheduledExecutorService executorScheduled = SpringContextUtil.getBean("scheduledExecutorService");

    /**
     * 单例模式
     */
    private AsyncManager(){}

    private static final AsyncManager instance = new AsyncManager();

    public static AsyncManager getInstance() {
        return instance;
    }

    /**
     * 执行任务
     * 
     * @param task 任务
     */
    public void execute(TimerTask task) {
        executorScheduled.schedule(task, 10, TimeUnit.MILLISECONDS);
    }

    /**
     * 停止任务线程池
     */
    public void shutdown() {
        Threads.shutdownAndAwaitTermination(executorScheduled);
        Threads.shutdownAndAwaitTermination(SpringContextUtil.getBean("logThreadExecutor"));
    }
}
